package ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Set;

import util.Stringer;

import model.Job;
import model.LMA;
import model.Volunteer;

/**
 * The applications Main Entry Point and UI code.
 * @author Andy Johnson
 * @author Justin Kudlo
 */
public class MainEntryPoint {
	/** The stringer. */
	public static Stringer theStringer;
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		model.System theSys;
		
		try {
			theStringer = new Stringer();
		} catch (IOException e) {
			//e.printStackTrace();
		}

		theSys = new model.System();

		DisplayMainMenu(theSys);
		
		//calls methods to serialize Users and Jobs
		theSys.Serialize();
	}

	/**
	 * Display main menu.
	 *
	 * @param theSystem the the system
	 */
	private static void DisplayMainMenu(
			model.System theSystem) {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		boolean doLoop = true;
		while(doLoop)
		{
			System.out.println(Stringer.getString(103));
			System.out.println();
			System.out.println(Stringer.getString(104));
			System.out.println(Stringer.getString(105));
			System.out.println(Stringer.getString(106));
			System.out.println(Stringer.getString(107));
			System.out.println(Stringer.getString(108));
			System.out.println();
			System.out.println(Stringer.getString(109));

			System.out.print(Stringer.getString(110));
			
			int selection = 0;
			try {
				String line = br.readLine();
				if(line.isEmpty())
				{
					continue;
				}
				selection = Integer.parseInt(line);
			} catch (IOException e) {
				//e.printStackTrace();
			}

			clearConsole();
			
			switch(selection)
			{
			case 1:
				DisplayLogin(theSystem);
				break;
			case 2:
				try {
					createUserAccount(theSystem);
				} catch (IOException e) {
				}
				break;
			case 5:
				doLoop = false;
				break;
			default:
				break;
			}
		}

	}

	/**
	 * Display login.
	 *
	 * @param theSystem the the system
	 */
	private static void DisplayLogin(
			model.System theSystem) {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		boolean loggedIn = false;
		while(!loggedIn)
		{
			System.out.println(Stringer.getString(111));
			System.out.println();
			System.out.println(Stringer.getString(112));
			System.out.println(Stringer.getString(113));
			System.out.println();
			System.out.print(Stringer.getString(114));
			String theUserName = "";
			try {
				theUserName = br.readLine();
				if(theUserName.isEmpty())
				{
					return;
				}
			} catch (IOException e) {
				//ignore invalid inputs...
				//e.printStackTrace();
			}

			if(theUserName.equalsIgnoreCase(
					Stringer.getString(115)))
			{
				return;
			}

			System.out.println();

			System.out.print(Stringer.getString(116));
			String thePassword = "";
			try {
				thePassword = br.readLine();
				
				if(thePassword.isEmpty())
				{
					return;
				}
				
			} catch (IOException e) {
				//ignore invalid input
				//e.printStackTrace();
			}
			System.out.println();
			
			loggedIn = theSystem.login(theUserName, 
					thePassword);
		
			clearConsole();
		}

		switch(theSystem.getCurrentUserType())
		{
		case Volunteer:
			DisplayVolunteerMenu(theSystem);
			break;
		case Scheduler:
			DisplaySchedulerMenu(theSystem);
			break;
		case LMA:
			DisplayLMAMenu(theSystem);
			break;
		}
	}

	/**
	 * Display lma menu.
	 *
	 * @param theSystem the the system
	 */
	private static void DisplayLMAMenu(
			model.System theSystem) {		
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		boolean doLoop = true;
		while(doLoop)
		{
			System.out.println(Stringer.getString(117));
			System.out.println();
			System.out.println(Stringer.getString(118));
			System.out.println(Stringer.getString(119));
			System.out.println(Stringer.getString(120));
			System.out.println(Stringer.getString(121));
			System.out.println(Stringer.getString(122));
			System.out.println(Stringer.getString(123));
			System.out.println();
			System.out.println(Stringer.getString(124));
			System.out.print(Stringer.getString(125));
			
			int selection = 0;
			try {
				String line = br.readLine();
				
				if(line.isEmpty())
				{
					return;
				}
				
				selection = Integer.parseInt(line);
			} catch (IOException e) {
				// ignore invalid input and repeat the menu.
				//e.printStackTrace();
			}

			switch(selection)
			{
			case 1:
				
				try {
					lmaSubmitsTMJ(theSystem);
				} catch (IOException e) {
					//e.printStackTrace();
				}
				break;
			case 4:
				doLoop = false;
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Display scheduler menu.
	 *
	 * @param theSystem the the system
	 */
	private static void DisplaySchedulerMenu(
			model.System theSystem) {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		boolean doLoop = true;
		while(doLoop)
		{
			System.out.println(Stringer.getString(126));
			System.out.println();
			System.out.println(Stringer.getString(127));
			System.out.println(Stringer.getString(128));
			System.out.println(Stringer.getString(129));
			System.out.println(Stringer.getString(130));
			System.out.println(Stringer.getString(131));
			System.out.println(Stringer.getString(132));
			System.out.println(Stringer.getString(133));
			System.out.println(Stringer.getString(134));
			System.out.println(Stringer.getString(135));
			System.out.println(Stringer.getString(136));
			System.out.println(Stringer.getString(137));
			System.out.println(Stringer.getString(138));
			System.out.println();
			System.out.println(Stringer.getString(139));
			System.out.print(Stringer.getString(140));
			
			int selection = 0;
			try {
				String line = br.readLine();
				if(line.isEmpty())
				{
					return;
				}
				selection = Integer.parseInt(line);
			} catch (IOException e) {
				// ignore invalid input and repeat the menu...
				//e.printStackTrace();
			}

			switch(selection)
			{
			case 4:
				try {
					schedulerSchedulingTMJ(theSystem);
				} catch (IOException e) {
					//e.printStackTrace();
				}
				break;
			case 11:
				doLoop = false;
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Display volunteer menu.
	 *
	 * @param theSystem the the system
	 */
	private static void DisplayVolunteerMenu(
			model.System theSystem) {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		
		boolean doLoop = true;
		while(doLoop)
		{
			System.out.println(Stringer.getString(141));
			System.out.println();
			System.out.println(Stringer.getString(142));
			System.out.println(Stringer.getString(143));
			System.out.println(Stringer.getString(144));
			System.out.println(Stringer.getString(145));
			System.out.println(Stringer.getString(146));
			System.out.println(Stringer.getString(147));
			System.out.println(Stringer.getString(148));
			System.out.println(Stringer.getString(149));
			System.out.println(Stringer.getString(150));
			System.out.println(Stringer.getString(151));
			System.out.println();
			System.out.println(Stringer.getString(152));
			System.out.print(Stringer.getString(153));

			int selection = 0;
			try {
				String line = br.readLine();
				if(line.isEmpty())
				{
					return;
				}
				selection = Integer.parseInt(line);
			} catch (IOException e) {
				// ignore invalid input and repeat the menu
				//e.printStackTrace();
			}

			switch(selection)
			{
			case 1:
				try {
					volunteerSignsUpForTMJ(theSystem);
				} catch (IOException e) {
					//e.printStackTrace();
				}
				break;
			case 10:
				doLoop = false;
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Creates the user account.
	 *
	 * @param theSystem the the system
	 * @throws IOException Signals that an I/O exception 
	 * 			has occurred.
	 */
	private static void createUserAccount(
			model.System theSystem) 
	throws IOException {

		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));

		System.out.println(Stringer.getString(154));
		System.out.println(Stringer.getString(155));
		System.out.println(Stringer.getString(156));
		System.out.println();

		System.out.println(Stringer.getString(157));
		String userName = br.readLine();

		if(userName.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(158));
		String password = br.readLine();

		if(password.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(159));
		String firstName = br.readLine();

		if(firstName.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(160));
		String lastName = br.readLine();

		if(lastName.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(161));
		String dob = br.readLine();

		if(dob.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(162));
		String num = br.readLine();

		if(num.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(163));
		String email = br.readLine();

		if(email.isEmpty())
		{
			return;
		}
		
		System.out.println(Stringer.getString(164));
		String test = br.readLine();

		if(test.isEmpty())
		{
			return;
		}
		
		System.out.println();

		clearConsole();
		
		if(test.charAt(0) == 'y' || 
				test.charAt(0) == 'Y')
		{
			theSystem.register(new Volunteer(firstName, 
					lastName, password, num, dob,email, 
					userName));
			
			System.out.println(Stringer.getString(165));
		}
		else if(test.charAt(0) == 'n' || 
				test.charAt(0) == 'N')
		{
			return;
		}
	}

	/**
	 * Scheduler scheduling tmj.
	 *
	 * @param theSystem the the system
	 * @throws IOException Signals that an I/O 
	 * exception has occurred.
	 */
	private static void schedulerSchedulingTMJ(
			model.System theSystem) 
	throws IOException {

		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		Job confirmedJob = null;

		while(confirmedJob == null)
		{
			System.out.println(Stringer.getString(166));
			System.out.println();
			
			HashMap<String, Job> someRequests = 
				theSystem.getAllRequests();

			Set<String> keys = someRequests.keySet();
			int index = 0;
			for(String s : keys)
			{
				System.out.println(index + 1 + ")" + s);
				System.out.println();
				index++;
			}

			System.out.println(Stringer.getString(167));
			System.out.println();
			
			System.out.print(Stringer.getString(168));
			String line = br.readLine();
			
			if(line.isEmpty())
			{
				return;
			}
			
			if(line.startsWith(Stringer.getString(169)) || 
			   line.startsWith(Stringer.getString(170)))
			{
				return;
			}
			
			int selection = Integer.parseInt(line);
			
			Job aJob = someRequests.get(
					(String)keys.toArray()[selection - 1]);
			
			System.out.println();

			System.out.println(Stringer.getString(171) + 
					selection + 
			Stringer.getString(172));

			System.out.println(Stringer.getString(173) + 
					aJob.getName());
			System.out.println(Stringer.getString(174) + 
					aJob.getDifficultyLevel());
			System.out.println(Stringer.getString(175) + 
					aJob.getStartMonth() + 
					Stringer.getString(176) + 
					aJob.getStartDay() + 
					Stringer.getString(177) + 
					aJob.getStartYear());
			System.out.println(Stringer.getString(178) + 
					aJob.getEndMonth() + 
					Stringer.getString(179) + 
					aJob.getEndDay() + 
					Stringer.getString(180) + 
					aJob.getEndYear());
			System.out.println(Stringer.getString(181) + 
					aJob.getTime());
			System.out.println(Stringer.getString(182) + 
					aJob.getDescription());
			System.out.println(Stringer.getString(183) + 
					aJob.getLMA().getFirstName() + " " + 
					aJob.getLMA().getLastName());
			System.out.println(Stringer.getString(184) + 
					aJob.getLMA().getPhoneNumber());

			System.out.println(Stringer.getString(185));
			System.out.println(Stringer.getString(186));	

			String confirm = br.readLine();
			
			if(confirm.isEmpty())
			{
				return;
			}

			if(confirm.charAt(0) == 'Y' || 
					confirm.charAt(0) == 'y')
			{
				confirmedJob = aJob;
			}
			else
			{
				continue;
			}
			
			System.out.println(Stringer.getString(187));
			int startYear = Integer.parseInt(br.readLine());
			
			System.out.println(Stringer.getString(188));
			int startMonth = Integer.parseInt(br.readLine());

			System.out.println(Stringer.getString(189));
			int startDate = Integer.parseInt(br.readLine());

			System.out.println(Stringer.getString(190));
			int endYear = Integer.parseInt(br.readLine());

			System.out.println(Stringer.getString(191));
			int endMonth = Integer.parseInt(br.readLine());

			System.out.println(Stringer.getString(192));
			int endDate = Integer.parseInt(br.readLine());
	
			theSystem.getJobs().scheduleJob(confirmedJob, 
					startYear, startMonth, startDate, 
					endYear, endMonth, endDate);
			
		}

		theSystem.getJobs().addNewJob(confirmedJob);

		System.out.println();
		System.out.println(Stringer.getString(193));
		System.out.println();
		
		clearConsole();
	}


	/**
	 * Volunteer signs up for tmj.
	 *
	 * @param theSystem the the system
	 * @throws IOException Signals that an I/O 
	 * exception has occurred.
	 */
	private static void volunteerSignsUpForTMJ(
			model.System theSystem) 
	throws IOException
	{
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		
		Job confirmedJob = null;
		while(confirmedJob == null)
		{
			System.out.println(Stringer.getString(194));
			HashMap<String, Job> someJobs = 
				theSystem.getAllJobs();

			Set<String> keys = someJobs.keySet();
			int index = 0;
			for(String s : keys)
			{
				System.out.println(index + 1 + ")" + s);
				System.out.println();
				index++;
			}

			System.out.println(Stringer.getString(195));

			System.out.print(Stringer.getString(196));
			
			String line = br.readLine();
			
			if(line.isEmpty())
			{
				return;
			}
			
			if(line.startsWith(Stringer.getString(197)) || 
			   line.startsWith(Stringer.getString(198)))
			{
				return;
			}
			
			int selection = Integer.parseInt(line);

			Job aJob = 
				someJobs.get(keys.toArray()[selection - 1]);

			System.out.println();

			System.out.println(Stringer.getString(199) + 
					selection + Stringer.getString(200));

			System.out.println(Stringer.getString(201) + 
					aJob.getName());
			System.out.println(Stringer.getString(202) + 
					aJob.getDifficultyLevel());
			System.out.println(Stringer.getString(203) + 
					aJob.getStartMonth() + 
					Stringer.getString(204) + 
					aJob.getStartDay() + 
					Stringer.getString(205) + 
					aJob.getStartYear());
			System.out.println(Stringer.getString(206) + 
					aJob.getEndMonth() + 
					Stringer.getString(207) + 
					aJob.getEndDay() + 
					Stringer.getString(208) + 
					aJob.getEndYear());
			System.out.println(Stringer.getString(209) + 
					aJob.getTime());
			System.out.println(Stringer.getString(210) + 
					aJob.getDescription());
			System.out.println(Stringer.getString(211) + 
					aJob.getLMA().getFirstName() + " " + 
					aJob.getLMA().getLastName());
			System.out.println(Stringer.getString(212) + 
					aJob.getLMA().getPhoneNumber());

			System.out.println(Stringer.getString(213));
			System.out.println(Stringer.getString(214));	

			String confirm = br.readLine();
			
			if(confirm.isEmpty())
			{
				return;
			}

			if(confirm.charAt(0) == 'Y' || confirm.charAt(0) == 'y')
			{
				confirmedJob = aJob;
				System.out.println(Stringer.getString(215));
				System.out.println();
			}
		}

		Volunteer theVol = (Volunteer)theSystem.getCurrentUser();
		confirmedJob.signupForJobAsVol(theVol);

	}


	/**
	 * Lma submits tmj.
	 *
	 * @param theSystem the the system
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	private static void lmaSubmitsTMJ(model.System theSystem) 
	throws IOException
	{
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));

		System.out.println(Stringer.getString(216));
		System.out.println();
		
		
		System.out.println(Stringer.getString(217));
		String jobName = br.readLine();

		System.out.println(Stringer.getString(218));
		String difficulty = br.readLine();
		int diff = 0;
		try {
			diff = Integer.parseInt(difficulty);
		}
		catch (Exception e) {
			//ignore exception
		}

		System.out.println(Stringer.getString(219));
		String hours = br.readLine();
		int hrs = 0;
		try {
			hrs = Integer.parseInt(hours);
		} catch (Exception e) {
			//ignore exception
		}

		System.out.print(Stringer.getString(220));
		String numVolunteers = br.readLine();
		if(numVolunteers.isEmpty())
		{
			return;
		}
		
		int numVol = Integer.parseInt(numVolunteers);
		
		System.out.print(Stringer.getString(221));
		String strNumGL = br.readLine();
		if(strNumGL.isEmpty())
		{
			return;
		}
		
		int numGL = Integer.parseInt(strNumGL);

		System.out.println(Stringer.getString(222));
		String description = br.readLine();

		if(description.isEmpty())
		{
			return;
		}
		
		Job newJob = new Job(jobName, diff, hrs, 
				(LMA)theSystem.getCurrentUser(),
				numGL, numVol, 
				description);

		System.out.println();
		System.out.println(Stringer.getString(223));
		System.out.println();
		
		System.out.println(Stringer.getString(224) + 
				newJob.getName());
		System.out.println(Stringer.getString(225) + 
				newJob.getDifficultyLevel());
		System.out.println(Stringer.getString(226) + 
				newJob.getTime());
		System.out.println(Stringer.getString(227) + 
				newJob.getMaxVolunteers());
		System.out.println(Stringer.getString(228) +
				newJob.getMaxGroupLeaders());
		System.out.println(Stringer.getString(229) + 
				newJob.getDescription());
		System.out.println();
		System.out.print(Stringer.getString(230));
		
		String line = br.readLine();
		
		if(line.isEmpty())
		{
			return;
		}
		
		if(!line.startsWith("y") || !line.startsWith("Y"))
		{
			return;
		}
		else
		{
			System.out.println(Stringer.getString(231));
			System.out.println();
		}
		
		theSystem.addRequest(newJob);
	}

	/**
	 * Clear console.
	 */
	private static void clearConsole()
	{
		//for(int i = 0; i < 80; i++)
		//{
		//	System.out.println();
		//}
	}

}

